#include "Terrain.h"


void terInitialiser(Terrain * pTer)
{ int i,j;
 char terrain_defaut[11][11]={

  "###########",
  "#_......._#",
  "#.........#",
  "#.........#",
  "#.........#",
  "#....*....#",
  "#.........#",
  "#.........#",
  "#.........#",
  "#_......._#",
  "###########"
  };
  pTer->dim=11;
  pTer->tab = (char **)malloc(sizeof(char *)*pTer->dim);
	for (j=0; j<pTer->dim; j++)
		pTer->tab[j] = (char *)malloc(sizeof(char)*pTer->dim);

	for(j=0;j<pTer->dim;j++)
		for(i=0;i<pTer->dim;i++)
			pTer->tab[j][i] = terrain_defaut[j][i];
}

void terLiberer(Terrain * pTer)
{
    int j;

	for (j=0; j<pTer->dim; j++)
		free(pTer->tab[j]);
	free(pTer->tab);

	pTer->dim = 0;
	pTer->tab = NULL;
}

const int terPositionValide(const Terrain * pTer, const int x,const int y)
{
    if(x>0 && y>0 && x<pTer->dim-1 && y<pTer->dim-1)
    {
       if(terGetXY(pTer,x,y)=='*' || terGetXY(pTer,x,y)=='_' || terGetXY(pTer,x,y)=='.') return 1;
    }
    return 0;

}

const char terGetXY(const Terrain * pTer, const int x,const int y)
{
    return pTer->tab[y][x];
}

void terSetXY(Terrain * pTer, const int x,const int y, const char lettre)
{
	pTer->tab[y][x] = lettre;
}

const int getDim(const Terrain * pTer)
{
    return pTer->dim;
}

void testRegresion_Terrain()
{
    Terrain ter;
    char c;

    c='\0';

    /*Test terInitialiser */
    terInitialiser(&ter);
    assert(ter.dim==11);
    assert(ter.tab!=NULL);
    printf("terInitialiser() OK...\n");

    assert(getDim(&ter)==11);
    printf("getDim() OK...\n");

    assert(terPositionValide(&ter,1,10)==0);
    assert(terPositionValide(&ter,1,1)==1);
    printf("terPositionValide OK...\n");

    terSetXY(&ter,1,1,'a');
    assert(ter.tab[1][1]=='a');
    printf("terSetXY() OK...\n");

    c=terGetXY(&ter,1,1);
    assert(c=='a');
    printf("terGetXY() OK...\n");

    terLiberer(&ter);
    assert(ter.dim==0 && ter.tab==NULL);
    printf("terLiberer() OK...\n");



}
